/**
 * 通讯对象
 * 定义了从数据库到页面的显示内容格式
 * 显示元素
 * 用于作为页面上HTML的输出
 */
package cn.antia.cwin;

import java.io.Serializable;
import java.util.*;

import cn.antia.cep.common.AntiaLog;

/**
 * @author bin
 * @version 0.1
 * @date Sep 30, 2006
 */
public class Cele implements Serializable,Comparator,Cloneable{
	private static final long serialVersionUID = 0;
	private Integer id; //ID
    private String owner;       //所有者(table or window or grid)
    private DBField field;      //数据库描述
    private HtmlWidget htmlWidget;  //HTML Widget
    private Output td;			//td的状态 =
    private Map<String,String> options; //选项

    private boolean isEncrypt;		//是否是加密字段
    
    public Cele() {
        field = new DBField();
        htmlWidget = new HtmlWidget();
        td = new Output();
    }

    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setId(Integer id){
		this.id = id;
	}
	public Integer getId(){
		return id;
	}

    public boolean isEncrypt() {
		return isEncrypt;
	}
	public void setEncrypt(boolean isEncrypt) {
		this.isEncrypt = isEncrypt;
	}

    public Output getTd() {
		return td;
	}
	public void setTd(Output td) {
		this.td = td;
	}

    public Map<String, String> getOptions() {
        return options;
    }

    public void setOptions(Map<String, String> options) {
        this.options = options;
    }

    public DBField getField() {
        return field;
    }

    public void setField(DBField field) {
        this.field = field;
    }

    public HtmlWidget getHtmlWidget() {
        return htmlWidget;
    }

    public void setHtmlWidget(HtmlWidget htmlWidget) {
        this.htmlWidget = htmlWidget;
    }

    public Cele clone()  {
		try{
			return (Cele)super.clone();
		}catch(Exception e){
			AntiaLog.error("拷贝ElementComsat对象出错！");
			return null;
		}
	}
	/* 
	 * 比较方法
	 */
	public int compare(Object o1, Object o2) {
		Cele ec1 = (Cele)o1;
		Cele ec2 = (Cele)o2;
        HtmlWidget hw1 = ec1.getHtmlWidget();
        HtmlWidget hw2 = ec2.getHtmlWidget();
        //index大的放后面
		if(hw1.getIndex() != null && hw2.getIndex() != null){
			if(hw1.getIndex() > hw2.getIndex()){
				return 1;
			}else if(hw1.getIndex().intValue() == hw2.getIndex().intValue()){
				return -1;
			}else{
				return 0;
			}
		}else if(hw1.getIndex() != null && hw2.getIndex() == null){
			return 1;
		}else if(hw1.getIndex() == null && hw2.getIndex() != null){
			return -1;
		}
		//textarea的放后面
		if(hw1.getHtmlType().toLowerCase().indexOf("textarea") > -1){
			return 1;
		}
		return 0;
	}
	
	/**
	 * 设置验证字符串
	 *
     * @param ec s*/
	public static void setValitionString(Cele ec){
		StringBuffer sb = new StringBuffer("");
        DBField field = ec.getField();
        //类型
		if(field.getValidType() != null){
			sb.append(";type:").append(field.getValidType());
		}
		//空判断
		if(field.getAllowNull() == null || !(field.getAllowNull())){
			if(field.getDefault() == null && field.getModifyValue() == null){
				sb.append(";required:true");
			}
		}
		//设置字符串长度
		if(field.getMinLen() != null || field.getMaxLen() != null){
			sb.append(";len:").append(field.getMinLen() == null ? "" : field.getMinLen().toString()).append("-").append(field.getMaxLen() == null ? "" : field.getMaxLen().toString());
		}
		//大小
		if(field.getMin() != null || field.getMax() != null){
			sb.append(";between:").append(field.getMin() == null ? "" : field.getMin().toString()).append("-").append(field.getMax() == null ? "" : field.getMax().toString());
		}
		//decLen
		if(field.getDecLen() != null){
			sb.append("decLen:").append(field.getDecLen());
		}
		//general
		if(!field.isGeneral()){
			sb.append(";general:false");
		}
		String limi = "";
		if(sb.length() > 1){
			limi = sb.substring(1);
		}
        HtmlWidget hw = ec.getHtmlWidget();
        hw.setLimits(limi);
	}

    public String getValitionString(){
		StringBuffer sb = new StringBuffer("");
        //类型
		if(field.getValidType() != null){
			sb.append(";type:").append(field.getValidType());
		}
		//空判断
		if(field.getAllowNull() == null || !(field.getAllowNull())){
			if(field.getDefault() == null && field.getModifyValue() == null){
				sb.append(";required:true");
			}
		}
		//设置字符串长度
		if(field.getMinLen() != null || field.getMaxLen() != null){
			sb.append(";len:").append(field.getMinLen() == null ? "" : field.getMinLen().toString()).append("-").append(field.getMaxLen() == null ? "" : field.getMaxLen().toString());
		}
		//大小
		if(field.getMin() != null || field.getMax() != null){
			sb.append(";between:").append(field.getMin() == null ? "" : field.getMin().toString()).append("-").append(field.getMax() == null ? "" : field.getMax().toString());
		}
		//decLen
		if(field.getDecLen() != null){
			sb.append("decLen:").append(field.getDecLen());
		}
		//general
		if(!field.isGeneral()){
			sb.append(";general:false");
		}
		String limi = "";
		if(sb.length() > 1){
			limi = sb.substring(1);
		}
        htmlWidget.setLimits(limi);
        return limi;
    }
}
